package com.demo.controller;

import com.demo.common.CreateDate;
import com.demo.common.CreateUUID;
import com.demo.common.ShaEncrypt;
import com.demo.model.SysManagers;
import com.demo.service.SysManagersService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Log4j2
public class SysManagersController {

    @Resource
    private SysManagersService service;

    @RequestMapping(value = "sys_managers_view")
    public String sys_managers_view(String loginname, String username, String cardid, String tel, HttpServletRequest request){
        //这里我要对从jsp传过来的数据进行一个清洗的任务
        if(null==loginname){ loginname = ""; }
        if(null==username){username = "";}
        if(null==cardid){cardid = "";}
        if(null==tel){tel = "";}
        SysManagers m = new SysManagers();
        m.setLoginname(loginname);
        m.setUsername(username);
        m.setCardid(cardid);
        m.setTel(tel);
        service.findAll(m,request);
        request.setAttribute("loginname",loginname);
        request.setAttribute("username",username);
        request.setAttribute("cardid",cardid);
        request.setAttribute("tel",tel);
        return "index/sys_managers/view";
    }

    @RequestMapping(value = "sys_managers_pre_save")
    public String sys_managers_pre_save(){
        return "index/sys_managers/save";
    }

    @RequestMapping(value = "sys_managers_save")
    public String sys_managers_save(SysManagers s,HttpServletRequest request){

        List<SysManagers> check_loginname_list = service.find_check_loginname(s.getLoginname());
        if(check_loginname_list.isEmpty()){
            String loginname = (String) request.getSession().getAttribute("loginname");
            String id = CreateUUID.getuuid();
            String datetimes = CreateDate.getDateHHMM();
            s.setPassword(ShaEncrypt.shaEncode(s.getPassword()));
            s.setId(id);
            s.setCreatename(loginname);
            s.setCreatetimes(datetimes);
            s.setOutname("");
            s.setOuttimes("");
            boolean b = service.save(s);
            request.setAttribute("b",b);
        }else{
            request.setAttribute("b",false);
        }

        return "index/sys_managers/save";
    }

    @RequestMapping(value = "sys_managers_findbyid")
    public String sys_managers_findbyid(String id,HttpServletRequest request){
        SysManagers s = service.findById(id);
        request.setAttribute("s",s);
        return "index/sys_managers/update";
    }

    @RequestMapping(value = "sys_managers_update")
    public String sys_managers_update(SysManagers s,HttpServletRequest request){
        boolean b = service.update(s);
        request.setAttribute("b",b);
        return "index/sys_managers/update";
    }

    @RequestMapping(value="sys_managers_del")
    public String sys_managers_del(String id){
        service.del(id);
        return "redirect:sys_managers_view";
    }

    @RequestMapping(value="sys_managers_open")
    public String sys_managers_open(String id){
        SysManagers s = new SysManagers();
        s.setId(id);
        s.setFlag("0");
        service.update_flag(s);
        return "redirect:sys_managers_view";
    }
    @RequestMapping(value="sys_managers_close")
    public String sys_managers_close(String id){
        SysManagers s = new SysManagers();
        s.setId(id);
        s.setFlag("1");
        service.update_flag(s);
        return "redirect:sys_managers_view";
    }
}
